package com.yxq.mdt.app.modules.personal.ui.fragment;

import android.content.Intent;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.modules.personal.adapter.MyReplyListAdapter;
import com.yxq.mdt.app.modules.personal.bean.CollectTopicListBean;
import com.yxq.mdt.app.modules.personal.bean.PublishBean;
import com.yxq.mdt.app.modules.personal.bean.TopicCommentBean;
import com.yxq.mdt.app.modules.personal.model.PublishModel;
import com.yxq.mdt.app.modules.personal.presenter.IPublishContract;
import com.yxq.mdt.app.modules.personal.presenter.PublishPresenter;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialStateDetailsAc;

import butterknife.BindView;
import cn.jzvd.Jzvd;

/**
 * @author L
 * @time 2019-11-19
 * @describe 我的发表-我的回帖
 */
public class MyPublishReplyFragment extends BaseFragment<PublishPresenter> implements IPublishContract.View {
    public static final String EXTRA_TEXT = "extra_text";
    @BindView(R.id.social_state_recyclerView)
    RecyclerView socialStateRecyclerView;
    MyReplyListAdapter mainSocialContactAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    protected int attachLayoutId() {
        return R.layout.fragment_social_state;
    }

    @Override
    protected PublishPresenter getPresenter() {
        return new PublishPresenter(this, new PublishModel());
    }

    @Override
    public void initView(View view) {
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.selectMyTopicComment(page, 20);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.selectMyTopicComment(page, 20);
        });

        socialStateRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mainSocialContactAdapter = new MyReplyListAdapter(R.layout.item_reply_publish_layout, null);
        socialStateRecyclerView.setAdapter(mainSocialContactAdapter);
        mainSocialContactAdapter.setOnItemClickListener((adapter, view1, position) -> {
            Intent it = new Intent(mContext, SocialStateDetailsAc.class);
            it.putExtra("topicId", mainSocialContactAdapter.getItem(position).getTopicId());
            it.putExtra("id", mainSocialContactAdapter.getItem(position).getTopicId());
            startActivity(it);
        });
        mainSocialContactAdapter.setOnItemChildClickListener((adapter, view12, position) -> {
            switch (view12.getId()) {
                case R.id.personal_item_img:
                    //跳转个人中心主页
                    Intent intent = new Intent(mContext, SocialPersonalHomepageAc.class);
                    intent.putExtra("userId", mainSocialContactAdapter.getItem(position).getUserId());
                    startActivity(intent);
                    break;
            }
        });
        socialStateRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(@NonNull View view) {
            }

            @Override
            public void onChildViewDetachedFromWindow(@NonNull View view) {
                try {
                    Jzvd jzvd = view.findViewById(R.id.item_social_video_player);
                    if (jzvd != null && Jzvd.CURRENT_JZVD != null && jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
                        if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
                            Jzvd.releaseAllVideos();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        mainSocialContactAdapter.setEmptyView(R.layout.layout_empty_view, socialStateRecyclerView);
        mainSocialContactAdapter.isUseEmpty(false);
    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                getActivity().finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onLoadListSuccess(PublishBean data) {

    }

    @Override
    public void onLoadListFailed(String msg) {

    }

    @Override
    public void onLoadToPicListSuccess(CollectTopicListBean data) {

    }

    @Override
    public void onLoadToPicListFailed(String msg) {

    }

    @Override
    public void onLoadTopicCommentListSuccess(TopicCommentBean data) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (page == 1) {
            mainSocialContactAdapter.setNewData(data.getList());
        } else {
            if (data.getList() != null && data.getList().size() != 0) {
                mainSocialContactAdapter.addData(data.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        mainSocialContactAdapter.isUseEmpty(!(mainSocialContactAdapter.getData().size() > 0));
    }

    @Override
    public void onLoadTopicCommentFailed(String msg) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

}
